# -*- coding: utf-8 -*- 
#
# Description:
#
#  Definiton
#
#
#
# Changes:
#
# 2012-01-10
# Initial Commit
#
#
# Copyright 2010-2012, WyDev Team.
# Author: Polo35 (polo35580@hotmail.fr)
#
# Licenced under Academic Free License version 3.0
# Review WyGui README & LICENSE files for further details.

nname: 0
n 0(None)[w, h, interlaced, freq = resolution
pygui_globs['display'].set_resolution((w, h), interlaced, freq)
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb7741e6c>}
nname: 114
n 114(None)[return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)
]:
	i: 0(AL), 112()
	o: 

nname: 112
n 112(None)[_POP_TOP pass
]:
	i: 57&77(f), 98()
	o: 114()

nname: 98
n 98(None)[index -= 1
]:
	i: 57&77(t)
	o: 112()

nname: 57&77
n 57&77(exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index))[]:
	i: 0(loop)
	o: 98(t), 112(f)

nname: 0
n 0(None)[index = 99999
join = os.path.join
exists = os.path.exists
psp_prefix = os.path.join(psp_path, 'video', 'M4V')
]:
	i: 
	o: 57&77(loop), 114(AL)

nname: 114
n 114(None)[return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)
]:
	i: 98()
	o: 

nname: 98
n 98(None)[	index -= 1
else:
	_POP_TOP pass
]:
	i: 0(t)
	o: 114()

nname: 0
n 0(exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index))[index = 99999
join = os.path.join
exists = os.path.exists
psp_prefix = os.path.join(psp_path, 'video', 'M4V')
while exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index):
]:
	i: 
	o: 98(t)

nname: 114
n 114(None)[return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)
]:
	i: 0()
	o: 

nname: 0
n 0(None)[index = 99999
join = os.path.join
exists = os.path.exists
psp_prefix = os.path.join(psp_path, 'video', 'M4V')
while exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index):
	index -= 1
else:
	_POP_TOP pass
]:
	i: 
	o: 114()

nname: 0
n 0(None)[index = 99999
join = os.path.join
exists = os.path.exists
psp_prefix = os.path.join(psp_path, 'video', 'M4V')
while exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index):
	index -= 1
else:
	_POP_TOP pass
return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb7748e6c>}
nname: 0
n 0(None)[psp_mp4_file, psp_thm_file = _get_psp_filenames(psp_path)
msg = MessageWindow(_('Copying to PSP (tm) please wait...'), button=None)
msg.show()
mp4_cmd = '/usr/bin/ts2psp "%s" "%s"' % (ts_file, psp_mp4_file)
thm_cmd = 'cp "%s" "%s"' % (thm_src_file, psp_thm_file)
log.debug(mp4_cmd)
os.system(mp4_cmd)
log.debug(thm_cmd)
os.system(thm_cmd)
os.system('sync')
sleep(2)
msg.hide()
return None
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb7748fec>}
nname: 53
n 53(None)[Task(_copy_ts, output_file, psp_path).start(0)
raise StopIteration()
]:
	i: 26(), 52()
	o: 

nname: 52
n 52(None)[]:
	i: 0(f)
	o: 53()

nname: 26
n 26(None)[Task(_set_resolution, previous_resolution).start(0.20000000000000001)
]:
	i: 0(t)
	o: 53()

nname: 0
n 0(change_resolution)[log.debug('transcode_stop()')
stop_method()
]:
	i: 
	o: 26(t), 52(f)

nname: 0
n 0(None)[log.debug('transcode_stop()')
stop_method()
if change_resolution:
	Task(_set_resolution, previous_resolution).start(0.20000000000000001)
Task(_copy_ts, output_file, psp_path).start(0)
raise StopIteration()
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb774808c>}
nname: 405
n 405(None)[return None
]:
	i: 270(), 404()
	o: 

nname: 404
n 404(None)[]:
	i: 151(f)
	o: 405()

nname: 270
n 270(None)[pygui_globs['menustack'].back_one_menu()
context['menu'].stop()
transcode_client_set_file(output_file)
item.execute()
louie_send('on_user_idle', player)
log.debug('transcode_start() output_file=%s' % output_file)
transcode_client_start()
stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
player.stop_transcode = stop_task.start
]:
	i: 255(), 269()
	o: 405()

nname: 269
n 269(None)[]:
	i: 157(f)
	o: 270()

nname: 255
n 255(None)[_set_resolution(transcode_resolution)
]:
	i: 157(t)
	o: 270()

nname: 157
n 157(change_resolution)[previous_resolution = user_config['connections']['resolution']
player = context['selected'].player
item = context['selected']
psp_path = context['psp_path'].split('//', 1)[1]
output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
]:
	i: 151(t)
	o: 255(t), 269(f)

nname: 151
n 151(transcode_client)[]:
	i: 19(), 114(), 138(), 150(AE)
	o: 157(t), 404(f)

nname: 138
n 138(None)[print 'Cannot load transcode.'
]:
	i: 104(f)
	o: 151()

nname: 114
n 114(None)[#, e
print 'Cannot load transcode',
print str(e)
]:
	i: 104(t)
	o: 151()

nname: 104
n 104(<dummy_ex3> EXC_MATCH Exception)[]:
	i: 0(except)
	o: 114(t), 138(f)

nname: 19
n 19(None)[transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
transcode_client_start = transcode_client.method('start', '', '')
transcode_client_stop = transcode_client.method('stop', '', '')
transcode_client_set_file = transcode_client.method('set_file', 's', '')
]:
	i: 0(try)
	o: 151()

nname: 0
n 0(None)[transcode_client = None
bus = pygui_globs['wydbus']
]:
	i: 
	o: 19(try), 104(except)

nname: 405
n 405(None)[return None
]:
	i: 157(), 104(f)
	o: 

nname: 157
n 157(None)[previous_resolution = user_config['connections']['resolution']
player = context['selected'].player
item = context['selected']
psp_path = context['psp_path'].split('//', 1)[1]
output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
if change_resolution:
	_set_resolution(transcode_resolution)
pygui_globs['menustack'].back_one_menu()
context['menu'].stop()
transcode_client_set_file(output_file)
item.execute()
louie_send('on_user_idle', player)
log.debug('transcode_start() output_file=%s' % output_file)
transcode_client_start()
stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
player.stop_transcode = stop_task.start
]:
	i: 104(t)
	o: 405()

nname: 104
n 104(transcode_client)[except Exception, e:
	print 'Cannot load transcode',
	print str(e)
except:
	print 'Cannot load transcode.'
]:
	i: 0()
	o: 157(t), 405(f)

nname: 0
n 0(None)[transcode_client = None
bus = pygui_globs['wydbus']
try:
	transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
	transcode_client_start = transcode_client.method('start', '', '')
	transcode_client_stop = transcode_client.method('stop', '', '')
	transcode_client_set_file = transcode_client.method('set_file', 's', '')
]:
	i: 
	o: 104()

nname: 405
n 405(None)[return None
]:
	i: 0(f)
	o: 

nname: 0
n 0(None)[transcode_client = None
bus = pygui_globs['wydbus']
try:
	transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
	transcode_client_start = transcode_client.method('start', '', '')
	transcode_client_stop = transcode_client.method('stop', '', '')
	transcode_client_set_file = transcode_client.method('set_file', 's', '')
except Exception, e:
	print 'Cannot load transcode',
	print str(e)
except:
	print 'Cannot load transcode.'
if transcode_client:
	previous_resolution = user_config['connections']['resolution']
	player = context['selected'].player
	item = context['selected']
	psp_path = context['psp_path'].split('//', 1)[1]
	output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
	if change_resolution:
		_set_resolution(transcode_resolution)
	pygui_globs['menustack'].back_one_menu()
	context['menu'].stop()
	transcode_client_set_file(output_file)
	item.execute()
	louie_send('on_user_idle', player)
	log.debug('transcode_start() output_file=%s' % output_file)
	transcode_client_start()
	stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
	player.stop_transcode = stop_task.start
]:
	i: 
	o: 405()

nname: 0
n 0(None)[transcode_client = None
bus = pygui_globs['wydbus']
try:
	transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
	transcode_client_start = transcode_client.method('start', '', '')
	transcode_client_stop = transcode_client.method('stop', '', '')
	transcode_client_set_file = transcode_client.method('set_file', 's', '')
except Exception, e:
	print 'Cannot load transcode',
	print str(e)
except:
	print 'Cannot load transcode.'
if transcode_client:
	previous_resolution = user_config['connections']['resolution']
	player = context['selected'].player
	item = context['selected']
	psp_path = context['psp_path'].split('//', 1)[1]
	output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
	if change_resolution:
		_set_resolution(transcode_resolution)
	pygui_globs['menustack'].back_one_menu()
	context['menu'].stop()
	transcode_client_set_file(output_file)
	item.execute()
	louie_send('on_user_idle', player)
	log.debug('transcode_start() output_file=%s' % output_file)
	transcode_client_start()
	stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
	player.stop_transcode = stop_task.start
return None
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb774d46c>}
nname: 0
n 0(None)[from __future__ import absolute_import
import os
from peewee.messages import send as louie_send
from pygui.shared import pygui_globs
from peewee.notifier import Task
from pygui.config import user_config, TMP_DIR
from pygui.window import MessageWindow
from time import sleep
from peewee.debug import GET_LOGGER
log = GET_LOGGER(__name__)
transcode_resolution = (720, 480, 1, 50)
change_resolution = True
thm_src_file = '/usr/share/pygui/skins/wybox/icons/mimetypes/psp_thumb_wyplay.thm'
def _set_resolution(resolution):
	w, h, interlaced, freq = resolution
	pygui_globs['display'].set_resolution((w, h), interlaced, freq)

def _get_psp_filenames(psp_path):
	index = 99999
	join = os.path.join
	exists = os.path.exists
	psp_prefix = os.path.join(psp_path, 'video', 'M4V')
	while exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index):
		index -= 1
	else:
		_POP_TOP pass
	return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)

def _copy_ts(ts_file, psp_path):
	psp_mp4_file, psp_thm_file = _get_psp_filenames(psp_path)
	msg = MessageWindow(_('Copying to PSP (tm) please wait...'), button=None)
	msg.show()
	mp4_cmd = '/usr/bin/ts2psp "%s" "%s"' % (ts_file, psp_mp4_file)
	thm_cmd = 'cp "%s" "%s"' % (thm_src_file, psp_thm_file)
	log.debug(mp4_cmd)
	os.system(mp4_cmd)
	log.debug(thm_cmd)
	os.system(thm_cmd)
	os.system('sync')
	sleep(2)
	msg.hide()
	return None

def _stop_transcode(stop_method, previous_resolution, player, output_file, psp_path):
	log.debug('transcode_stop()')
	stop_method()
	if change_resolution:
		Task(_set_resolution, previous_resolution).start(0.20000000000000001)
	Task(_copy_ts, output_file, psp_path).start(0)
	raise StopIteration()

def transcode(context):
	transcode_client = None
	bus = pygui_globs['wydbus']
	try:
		transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
		transcode_client_start = transcode_client.method('start', '', '')
		transcode_client_stop = transcode_client.method('stop', '', '')
		transcode_client_set_file = transcode_client.method('set_file', 's', '')
	except Exception, e:
		print 'Cannot load transcode',
		print str(e)
	except:
		print 'Cannot load transcode.'
	if transcode_client:
		previous_resolution = user_config['connections']['resolution']
		player = context['selected'].player
		item = context['selected']
		psp_path = context['psp_path'].split('//', 1)[1]
		output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
		if change_resolution:
			_set_resolution(transcode_resolution)
		pygui_globs['menustack'].back_one_menu()
		context['menu'].stop()
		transcode_client_set_file(output_file)
		item.execute()
		louie_send('on_user_idle', player)
		log.debug('transcode_start() output_file=%s' % output_file)
		transcode_client_start()
		stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
		player.stop_transcode = stop_task.start
	return None

]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb7739c8c>}
from __future__ import absolute_import
import os
from peewee.messages import send as louie_send
from pygui.shared import pygui_globs
from peewee.notifier import Task
from pygui.config import user_config, TMP_DIR
from pygui.window import MessageWindow
from time import sleep
from peewee.debug import GET_LOGGER
log = GET_LOGGER(__name__)
transcode_resolution = (720, 480, 1, 50)
change_resolution = True
thm_src_file = '/usr/share/pygui/skins/wybox/icons/mimetypes/psp_thumb_wyplay.thm'
def _set_resolution(resolution):
	w, h, interlaced, freq = resolution
	pygui_globs['display'].set_resolution((w, h), interlaced, freq)

def _get_psp_filenames(psp_path):
	index = 99999
	join = os.path.join
	exists = os.path.exists
	psp_prefix = os.path.join(psp_path, 'video', 'M4V')
	while exists(psp_prefix + '%s.MP4' % index) or exists(psp_prefix + '%s.THM' % index):
		index -= 1
	else:
		_POP_TOP pass
	return (psp_prefix + '%s.MP4' % index, psp_prefix + '%s.THM' % index)

def _copy_ts(ts_file, psp_path):
	psp_mp4_file, psp_thm_file = _get_psp_filenames(psp_path)
	msg = MessageWindow(_('Copying to PSP (tm) please wait...'), button=None)
	msg.show()
	mp4_cmd = '/usr/bin/ts2psp "%s" "%s"' % (ts_file, psp_mp4_file)
	thm_cmd = 'cp "%s" "%s"' % (thm_src_file, psp_thm_file)
	log.debug(mp4_cmd)
	os.system(mp4_cmd)
	log.debug(thm_cmd)
	os.system(thm_cmd)
	os.system('sync')
	sleep(2)
	msg.hide()
	return None

def _stop_transcode(stop_method, previous_resolution, player, output_file, psp_path):
	log.debug('transcode_stop()')
	stop_method()
	if change_resolution:
		Task(_set_resolution, previous_resolution).start(0.20000000000000001)
	Task(_copy_ts, output_file, psp_path).start(0)
	raise StopIteration()

def transcode(context):
	transcode_client = None
	bus = pygui_globs['wydbus']
	try:
		transcode_client = bus.reference('com.wyplay.record', '/com/wyplay/record_status')
		transcode_client_start = transcode_client.method('start', '', '')
		transcode_client_stop = transcode_client.method('stop', '', '')
		transcode_client_set_file = transcode_client.method('set_file', 's', '')
	except Exception, e:
		print 'Cannot load transcode',
		print str(e)
	except:
		print 'Cannot load transcode.'
	if transcode_client:
		previous_resolution = user_config['connections']['resolution']
		player = context['selected'].player
		item = context['selected']
		psp_path = context['psp_path'].split('//', 1)[1]
		output_file = os.path.join(TMP_DIR, 'transcode_%s.ts' % item.name)
		if change_resolution:
			_set_resolution(transcode_resolution)
		pygui_globs['menustack'].back_one_menu()
		context['menu'].stop()
		transcode_client_set_file(output_file)
		item.execute()
		louie_send('on_user_idle', player)
		log.debug('transcode_start() output_file=%s' % output_file)
		transcode_client_start()
		stop_task = Task(_stop_transcode, transcode_client_stop, previous_resolution, context['selected'].player, output_file, psp_path)
		player.stop_transcode = stop_task.start
	return None

